package com.dynamicapp.plugin

import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import com.dynamicapp.R
import xu.plugin.PluginInterface

class ProxyActivity : FragmentActivity() {
    companion object {
        fun launch(context: Context?, path: String?) {
            PluginManager.instance.getResourcesPackageInfo(context, path).also {
                if (it?.activities?.isNotEmpty() == true) {
                    val intent = Intent(context, ProxyActivity::class.java)
                    intent.putExtra("path", path)
                    intent.putExtra("className", it.activities?.first()?.name)
                    context?.startActivity(intent)
                }
            }
        }
    }

    private var pluginInterface: PluginInterface? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)

        val path = intent.getStringExtra("path")
        val className = intent.getStringExtra("className")  //拿到要启动的Activity

        if (path?.isNotEmpty() == true) {
            Thread {
                PluginManager.instance.loadApk(this, path)
                runOnUiThread {
                    launchActivity(className)
                }
            }.start()
        } else {
            launchActivity(className)
        }
    }

    private fun launchActivity(className: String?) {
        if (className?.isEmpty() == true) return

        try {
            //加载该Activity的字节码对象
            val aClass = PluginManager.instance.pluginDexClassLoader?.loadClass(className)
            //创建该Activity的示例
            val newInstance = aClass?.newInstance()
            //程序健壮性检查
            if (newInstance is PluginInterface) {
                pluginInterface = newInstance
                //将代理Activity的实例传递给三方Activity
                pluginInterface?.attachContext(this)
                //创建bundle用来与三方apk传输数据
                val bundle = Bundle()
                //调用三方Activity的onCreate，
                pluginInterface?.onCreate(bundle)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun getResources(): Resources? {
        return PluginManager.instance.pluginResources
    }

    override fun startActivity(intent: Intent?) {
        val newIntent = Intent(this, ProxyActivity::class.java)
        newIntent.putExtra("className", intent?.component?.className)
        super.startActivity(newIntent)
    }

    override fun onStart() {
        super.onStart()
        pluginInterface?.onStart()
    }

    override fun onRestart() {
        super.onRestart()
        pluginInterface?.onRestart()
    }

    override fun onResume() {
        super.onResume()
        pluginInterface?.onResume()
    }

    override fun onPause() {
        super.onPause()
        pluginInterface?.onPause()
    }

    override fun onStop() {
        super.onStop()
        pluginInterface?.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        pluginInterface?.onDestroy()
    }
}